<?php

namespace app\common\model;

use think\Exception;
use think\Model;


class Commission extends Model
{

    // 表名
    protected $table = 'commission';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];

    public static function init()
    {
        self::beforeInsert(function ($row) {
            if ($row['min'] < 0) {
                throw new Exception("最小人数不小于0");
            }

            if ($row['max'] < 0) {
                throw new Exception("最小大数不小于0");
            }

            if ($row['min'] > $row['max'] && $row['max'] != 0) {
                throw new Exception("最小人数不能大于最大人数");
            }

            if ($row['ratio'] < 0 || $row['ratio'] > 100) {
                throw new Exception("比例保持在0~100以内");
            }
        });

        self::beforeUpdate(function ($row) {
            if (isset($row['ratio'])) {
                if ($row['ratio'] < 0 || $row['ratio'] > 100) {
                    throw new Exception("比例保持在0~100以内");
                }
            }

            if ($row['min'] < 0) {
                throw new Exception("最小人数不小于0");
            }

            if ($row['max'] < 0) {
                throw new Exception("最小大数不小于0");
            }

            if ($row['min'] > $row['max'] && $row['max'] != 0) {
                throw new Exception("最小人数不能大于最大人数");
            }
        });
    }

    public static function ratio($parent_id_one, $service)
    {
        $ratio_one = 0;
        $ratio_two = 0;
        $parent_one = User::where('id', $parent_id_one)->find();
        if ($parent_one) {//上级判断
            $parent_one_num = User::where('parent_id', $parent_id_one)->count();//邀请人数
            $ratio_one = self::where(['min' => ['<=', $parent_one_num], 'status' => 'agency'])->order('min DESC')->value('ratio', 0);

            //总比例
            $all_ratio = Config::where('name', 'max_commission')->value('value', 60);
            $parent_two = User::where('id', $parent_one['parent_id'])->find();
            if ($parent_two) {//二级判断
                if ($parent_one['is_club'] == 1 && $parent_two['is_club'] != 1) {//是一级俱乐部,无二级俱乐部
                    $ratio_one = $all_ratio;
                }

                if ($parent_two['is_club'] == 1) {//二级俱乐部
                    $ratio_two = $all_ratio - $ratio_one;

                    if ($parent_two['is_club'] == 1 && $parent_one['is_club'] == 1) {//一级二级同俱乐部
                        $parent_two_num = User::where('parent_id', $parent_one['parent_id'])->where('')->count();//邀请人数
                        $ratio_two = self::where(['min' => ['<=', $parent_two_num], 'status' => 'club'])->order('min DESC')->value('ratio', 0);//二级代理比例
                        $ratio_one = $all_ratio - $ratio_two;
                    }
                }
            } else {
                //一级俱乐部奖励
                if ($parent_one['is_club'] == 1) {
                    $ratio_one = $all_ratio;
                }
            }
        }
        $ratio_one_jz = $ratio_one / 100;
        $ratio_two_jz = $ratio_two / 100;
        return ['one' => bcmul($ratio_one_jz, $service, 2), 'two' => bcmul($ratio_two_jz, $service, 2)];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusList()
    {
        return ['agency' => __('代理分佣'), 'club' => '俱乐部分佣'];
    }

}
